//
//  ZXImageMessageCell.m
//  ZXDNLLTest
//
//  Created by mxsm on 16/5/18.
//  Copyright © 2016年 mxsm. All rights reserved.
//

#import "ZXImageMessageCell.h"
#import "MSSBrowseModel.h"
#import "MSSBrowseNetworkViewController.h"
@implementation ZXImageMessageCell

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self insertSubview:self.messageImageView belowSubview:self.messageBackgroundImageView];
    }
    return self;
}

- (void) layoutSubviews
{
    [super layoutSubviews];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickBigImage)];
    [self.messageBackgroundImageView addGestureRecognizer:tap];
    
    
    float y = self.avatarImageView.originY +22;
    if (self.messageModel.ownerTyper == ZXMessageOwnerTypeSelf) {
        float x = self.avatarImageView.originX - self.messageImageView.frameWidth - 5;
        
        [self.messageImageView setOrigin:CGPointMake(x , y)];
        [self.messageBackgroundImageView setFrame:CGRectMake(x, y, self.messageModel.messageSize.width+ 10, self.messageModel.messageSize.height + 10)];
    }
    else if (self.messageModel.ownerTyper == ZXMessageOwnerTypeOther) {
        float x = self.avatarImageView.originX + self.avatarImageView.frameWidth + 5;
        [self.messageImageView setOrigin:CGPointMake(x, y)];
        [self.messageBackgroundImageView setFrame:CGRectMake(x, y, self.messageModel.messageSize.width+ 10, self.messageModel.messageSize.height + 10)];
    }
}

- (void)onclickBigImage{
    MSSBrowseModel *browseItem = [[MSSBrowseModel alloc]init];
    browseItem.smallImageView = self.messageImageView;// 小图
    NSString *strImageUrl=self.messageModel.imageURL;
    if ([strImageUrl containsString:@"_Min"]) {
        strImageUrl = [strImageUrl stringByReplacingOccurrencesOfString:@"_Min" withString:@""];
    }
    if (strImageUrl) {
        browseItem.bigImageUrl =[NSString stringWithFormat:@"%@", strImageUrl];
    }else{
        browseItem.bigImage=self.messageModel.image;
    }
    // 加载网络图片大图地址
    MSSBrowseNetworkViewController *bvc = [[MSSBrowseNetworkViewController alloc]initWithBrowseItemArray:@[browseItem] currentIndex:0];
    [bvc showBrowseViewController];
}


#pragma mark - Getter and Setter
-(void)setMessageModel:(ZXMessageModel *)messageModel
{
    [super setMessageModel:messageModel];
    if ([messageModel.image isKindOfClass:[UIImage class]]) {
        [self.messageImageView setImage:messageModel.image];
    }else{
        [self.messageImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",messageModel.imageURL]]];
    }
    [self.messageImageView setSize:CGSizeMake(messageModel.messageSize.width + 10, messageModel.messageSize.height + 10)];
    
    switch (self.messageModel.ownerTyper) {
        case ZXMessageOwnerTypeSelf:
            self.messageBackgroundImageView.image = [[UIImage imageNamed:@"message_sender_background_reversed"] resizableImageWithCapInsets:UIEdgeInsetsMake(28, 20, 15, 20) resizingMode:UIImageResizingModeStretch];
            break;
        case ZXMessageOwnerTypeOther:
            [self.messageBackgroundImageView setImage:[[UIImage imageNamed:@"message_receiver_background_reversed"] resizableImageWithCapInsets:UIEdgeInsetsMake(28, 20, 15, 20) resizingMode:UIImageResizingModeStretch]];
            break;
        default:
            break;
    }
    
}

- (UIImageView *) messageImageView
{
    if (_messageImageView == nil) {
        _messageImageView = [[UIImageView alloc] init];
        [_messageImageView setContentMode:UIViewContentModeScaleAspectFill];
        [_messageImageView setClipsToBounds:YES];
    }
    return _messageImageView;
}

@end
